﻿#region Copyrights Notice
//©2010 Microsoft Corporation. All rights reserved.
#endregion

#region usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Microsoft.InformationSecurity.CISF.SecurityPortal.Business.Entities;
using Microsoft.InformationSecurity.CISF.SecurityPortal.Business.Widget;
using Microsoft.InformationSecurity.CISF.SecurityPortal.UI.Web.PortalService;
#endregion

namespace Microsoft.InformationSecurity.CISF.SecurityPortal.UI.Web.Widgets.MasterControls
{
    public partial class ContainerUserControl : System.Web.UI.UserControl, IWidgetContainer
    {
        public WidgetContainerBase ac = new WidgetContainerBase();
        public event Action<WidgetInstance> OnControlDelete;

        //getting user name from context
        string _UserName = Environment.UserName;

        public bool ShowSettings
        {
            get
            {
                object val = ViewState[this.ClientID + "ShowDisplaySettings"] ?? false;
                return (bool)val;
            }
            set { ViewState[this.ClientID + "ShowDisplaySettings"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            ContainerHeader1.Super = this as IWidgetActions;
            ContainerHeader1.Title.Text = Microsoft.Security.Application.AntiXss.HtmlEncode(ac.WidgetInstanceControl.Caption);
            ShowAndHide();
            LinkButton b = ContainerHeader1.CloseLinkButton;
            b.OnClientClick = "if (confirm('Are you sure you want to remove the widget from your home page?')) { return deleteWidget('" + ac.ctrlWidgetInstanceControl.WidgetInstanceId + "'); } else { return false;}";
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            String vPath = ac.GetControlRelativeUrl();
            int nControlId = ac.GetControlID2();
            var currentControl = WidgetControlFactory.CreateWidget(this, nControlId, vPath);
            IWidget widget = currentControl as IWidget;
            MainDisplayArea.Controls.Add(currentControl);
            this.ac.ctrlWidgetControl = widget;
            
            //Adding Widget URL Mapping here
            if (vPath != "")
                this.ac.widgetPath = vPath.Substring(0, vPath.LastIndexOf('\\'));
            
            widget.OnControlInitilize(this);
        }
        private void ShowAndHide()
        {
            MainDisplayArea.Visible = ac.WidgetInstanceControl.IsExpanded;
            ContainerHeader1.ExpandLinkButton.Visible = !ac.WidgetInstanceControl.IsExpanded;
            ContainerHeader1.CollapseLinkButton.Visible = ac.WidgetInstanceControl.IsExpanded;
        }
        protected void EditButton_Click(object sender, EventArgs e)
        {
            UpdateSetting();

        }
        public void DoMinimize()
        {
            (this as IWidgetContainer).Minimize();
        }
        public void DoMaximize()
        {
            (this as IWidgetContainer).Maximize();
        }
        public void DoClose()
        {
            this.ac.ctrlWidgetControl.Close();
            (this as IWidgetContainer).Close();
        }
        protected void SaveSettings_Click(object sender, EventArgs e)
        {
            UpdateSetting();
        }
        protected void CancelSettings_Click(object sender, EventArgs e)
        {
            UpdateSetting();
        }
        public void UpdateSetting()
        {
            (this as IWidgetContainer).DisplaySettings(!this.ShowSettings);
            WidgetBodyUpdatePanel.Update();
        }
        void IWidgetActions.UpdateSettings()
        {
            UpdateSetting();
        }
        protected void RevertSetting()
        {
            IWidgetContainer c = this;
            c.DisplaySettings(!this.ShowSettings);
            WidgetBodyUpdatePanel.Update();
        }

        /* Begin - Implement IWidgetContainer */
        int IWidgetContainer.ControlID
        {
            get
            {
                return this.ac.WidgetInstanceControl.WidgetInstanceId;
            }
        }

        void IWidgetContainer.Save(string state)
        {
            //Adding SOA Layer
            PortalServiceClient wscProxy = new PortalServiceClient();
            if (!wscProxy.UpdateWidgetState(_UserName, ac.WidgetInstanceControl.WidgetId, state)) { } //Success
            else { }//Failure.
            wscProxy.Close();
        }
        void IWidgetActions.Minimize()
        {
            //Adding SOA Layer
            PortalServiceClient wscProxy = new PortalServiceClient();
            if (!wscProxy.MinimizeWidget(_UserName, ac.WidgetInstanceControl.WidgetId))
            {
                ac.WidgetInstanceControl.IsExpanded = false;
            }//Success
            else { }//Failure.
            wscProxy.Close();

            this.ShowAndHide();

            WidgetBodyUpdatePanel.Update();
            WidgetHeaderUpdatePanel.Update();
        }

        void IWidgetActions.Maximize()
        {
            //Adding SOA Layer
            PortalServiceClient wscProxy = new PortalServiceClient();
            if (!wscProxy.MaximizeWidget(_UserName, ac.WidgetInstanceControl.WidgetId))
            {
                ac.WidgetInstanceControl.IsExpanded = true;
            }//Success
            else { }//Failure.
            wscProxy.Close();

            ShowAndHide();

            WidgetBodyUpdatePanel.Update();
            WidgetHeaderUpdatePanel.Update();
        }
        void IWidgetActions.Close()
        {
            //Adding SOA Layer
            PortalServiceClient wscProxy = new PortalServiceClient();
            if (!wscProxy.DeleteWidget(_UserName, ac.WidgetInstanceControl.WidgetId)){}//Success
            else { }//Failure.
            wscProxy.Close();

            //Update UI.
            OnControlDelete(ac.WidgetInstanceControl);
        }

        string IWidgetContainer.Restore()
        {
            return ac.WidgetInstanceControl.State;
        }
        void IWidgetActions.MaximizeToFullScreen()
        {
            //Empty Implementation
        }
        void IWidgetActions.DisplaySettings(bool bShow)
        {
            this.ShowSettings = bShow;
            ac.ctrlWidgetControl.DisplaySettings(bShow);

            if (bShow)
            {
                (this as IWidgetContainer).Maximize();
            }

            this.WidgetHeaderUpdatePanel.Update();
        }
        public bool FirstTime
        {
            get { return ac.bLoadFirstTime; }
            set { ac.bLoadFirstTime = value; }
        }
        public string WidgetPath
        {
            get { return ac.widgetPath; }
            set { ac.widgetPath = value; }
        }
    }
}